package com.imooc.course.generator.server;

import com.imooc.course.generator.util.DbUtil;
import com.imooc.course.generator.util.Field;
import com.imooc.course.generator.util.FreemarkerUtil;
import com.imooc.course.generator.util.ReadGeneratorConfigXml;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BusinessGenerator {
    /**
     * 需要手动配置的静态变量
     */
    //占位符//同时也是模块变量
    private static String model = "business";
    //生成的文件在这里
    private static String toPath = model+"\\src\\main\\java\\com\\imooc\\course\\"+model;
    //generatorConfig.xml文件的位置的路径
    private static String generatorConfigPath = "server\\src\\main\\resources\\generator\\generatorConfig.xml";

    /**
     * 自动配置的静态变量
     * 通过遍历generatorConfig.xml完成
     */
    //占位符
    private static String Domain;
    private static String domain;
    private static String tabNameCn;
    private static String TABLE_NAME;
    private static List<Field> fieldList;
    private static Set<String> typeSet;

    public static void main(String[] args) throws Exception {
        init(generatorConfigPath);

        Map map = getMap();

        form(map);
        controller(map);
    }

    private static void form(Map map) throws IOException, TemplateException {
        //配置文件位置
        String path = "business\\form\\";
        //使用哪个配置文件
        String ftl = "form.ftl";
        //DTO生成目标路径
        String toFormPath = toPath+"\\form\\";

        FreemarkerUtil.init(path)
                .config(ftl)
                .generator(toFormPath + Domain + "Form.java",map);
    }

    private static void controller(Map map) throws IOException, TemplateException {
        //配置文件位置
        String path = "business\\controller\\";
        //使用哪个配置文件
        String ftl = "controller.ftl";
        //生成文件的终点位置路径段//
        String targetPath = "\\controller\\admin\\";

        FreemarkerUtil.init(path)
                .config(ftl)
                .generator(toPath+targetPath+Domain+"Controller.java",map);
    }

    private static Map getMap() throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("Domain", Domain);
        map.put("domain", domain);
        map.put("model", model);
        map.put("modelName", TABLE_NAME);
        map.put("tabNameCn", tabNameCn);
        map.put("fieldList", fieldList);
        map.put("typeSet", typeSet);

        return map;
    }

    private static void init(String generatorConfigPath) throws Exception {
        ReadGeneratorConfigXml init = ReadGeneratorConfigXml.init(generatorConfigPath);
        Domain = init.getDomainObjectName();
        domain = init.getLowerCaseDomain();
        tabNameCn = init.getTableComment();
        TABLE_NAME = model.toUpperCase();
        //从数据库中获取所有的列和所有列的注释
        fieldList = DbUtil.getColumnByTableName(init.getTableName());
        typeSet = DbUtil.getJavaTypes(fieldList);
    }

}
